/*
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-05-16 10:42:03
 * @Description  : 当前导航的nav
 */

export const useNav = () => {
  const historyNavs = ref<any[]>([])
  const curNav = ref<any>(undefined)
  const navIndex = ref(0)
  const viewModel = ref<'list' | 'grid'>('list')

  const canforword = computed(() => {
    return navIndex.value < historyNavs.value.length - 1
  })

  const canBack = computed(() => {
    return navIndex.value > 0
  })

  const showDesc = ref('1')

  const setShowSourceDesc = (desc: string) => {
    showDesc.value = desc
  }

  const setHistoryNav = (navs: any[]) => {
    historyNavs.value = navs
  }

  const setSourceViewModel = (model: 'list' | 'grid') => {
    viewModel.value = model
  }

  const pushHistoryNav = (nav: any) => {
    if (
      historyNavs.value.length &&
      historyNavs.value[historyNavs.value.length - 1].key === nav.key
    ) {
      return
    }
    historyNavs.value.push(nav)
    navIndex.value = historyNavs.value.length - 1
    setCurNav(historyNavs.value[navIndex.value])
  }

  const setCurNav = (nav: any) => {
    curNav.value = nav
  }

  const backNavIndex = () => {
    if (navIndex.value <= 0) {
      return
    }
    navIndex.value -= 1
    if (navIndex.value < historyNavs.value.length) {
      setCurNav(historyNavs.value[navIndex.value])
    }
  }

  const nextNavIndex = () => {
    if (navIndex.value >= historyNavs.value.length - 1) {
      return
    }
    navIndex.value += 1
    setCurNav(historyNavs.value[navIndex.value])
  }

  return {
    curNav,
    historyNavs,
    pushHistoryNav,
    setCurNav,
    backNavIndex,
    nextNavIndex,
    setSourceViewModel,
    setShowSourceDesc,
    showDesc,
    canBack,
    canforword,
    viewModel
  }
}
